// 导入的三个辅助函数都是用来监听 Action 的，只要 action 发送过来，就会触发相应的 saga 函数调用
import { takeEvery, takeLatest, throttle, select, call, take, put } from 'redux-saga/effects'
// 如果是请求服务器的数据则引入 axios
import axios from 'axios'

export function* defSaga() {
    // console.log('defSaga');
    yield takeEvery('TakeEvery', function*() {
        // const state = yield select()
        // console.log('触发takeEvery', state);
        // 获取 home 里面的数据
        const user = yield select((state)=>(state.user))
        // console.log('触发takeEvery', user);

        //模拟请求外部服务器数据，使用 axios 
        const res = yield call(axios.post, 'http://127.0.0.1:7002/login',{...user})
        console.log('TakeEvery', res);
        yield put({
            type: 'login_success',
            ...res.data
        })
    })
    yield takeLatest('TakeLatest', function*() {
        // const state = yield select()
        // console.log('TakeLatest', state);
        // 获取 home 里面的数据
        const user = yield select((state)=>(state.user))
        // console.log('TakeLatest', user);
        //模拟请求外部服务器数据，使用 axios 
        const res = yield call(axios.post, 'http://127.0.0.1:7002/login',{...user})
        console.log('TakeLatest', res);
    })
    yield throttle(0,'Throttle', function*() {
        // const state = yield select()
        // console.log('Throttle', state);
        // 获取 home 里面的数据
        const user = yield select((state)=>(state.user))
        //模拟请求外部服务器数据，使用 axios 
        const res = yield call(axios.post, 'http://127.0.0.1:7002/login',{...user})
        console.log('Throttle', res);
    })

    yield take('take') // 阻塞在这里
    console.log('阻塞take');
    
     
     
}